package com.example.smartcitymovie.ui.selfcenter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.bean.OrdersListBean;
import com.example.smartcitymovie.databinding.ActivityOrderListBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class OrderListActivity extends AppCompatActivity {

    ActivityOrderListBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityOrderListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getSupportActionBar().setTitle("订单列表");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        initView();
    }

    private void initView() {
        OkHttpUtils.getCall("/prod-api/api/allorder/list").enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                OrdersListBean ordersListBean = MyApplication.getGson().fromJson(response.body().string(),OrdersListBean.class);
                if (ordersListBean.getCode()==200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            binding.RVOrderList.setLinearLayoutManager(LinearLayout.VERTICAL);
                            binding.RVOrderList.setAdapter(ordersListBean.getRows(),R.layout.order_list_item_layout);
                        }
                    });
                }
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}